package controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import dao.FileDao;
import dto.FileDto;
import dto.UserDto;
import service.SendMailService;
import service.SendMailServiceImpl;
import service.UserService;

@Controller
public class UserController {

	@Autowired
	private UserService userService;

	@Autowired
	private SendMailService sendMailService;

	@Autowired
	private FileDao fileDao;

	@RequestMapping(value = "/insertUser.html", method = RequestMethod.POST)
	public ModelAndView insertUser(UserDto bean, HttpServletRequest request,
			HttpServletResponse response) {
		ModelAndView modelAndView = new ModelAndView();
		Map<String, Object> model = new HashMap<String, Object>();

		String email = request.getParameter("email1") + "@"
				+ request.getParameter("email2");
		String phone = request.getParameter("phone1") + "-"
				+ request.getParameter("phone2") + "-"
				+ request.getParameter("phone3");
		bean.setEmail(email);
		bean.setPhone(phone);
		int result = this.userService.insertUser(bean);
		if (result > 0) {
			model.put("insert_id", bean.getName());// insert_result는 프로토콜
			model.put("content", "/WEB-INF/member/joinSuccessForm.jsp");
			modelAndView.setViewName("index");
		} else {
			model.put("content", "/WEB-INF/errorpage/errorForm.jsp");
			modelAndView.setViewName("index");
		}
		modelAndView.addAllObjects(model);
		return modelAndView;
	}

	@RequestMapping(value = "/loginUser.html", method = RequestMethod.POST)
	public ModelAndView loginUser(UserDto bean, HttpSession session) {
		ModelAndView modelAndView = new ModelAndView();
		FileDto filedto = null;
		int protocol = 0;
		Map<String, Object> model = new HashMap<String, Object>();
			filedto = this.fileDao.selectOne(bean.getId());

			if(filedto == null){
				protocol= 1;
			}else{
				protocol =0;
			}
		try {
			System.out.println(bean.getId() + ", " + bean.getPasswd());
			if (bean.getId().equals("dream")
					&& bean.getPasswd().equals("cometrue")) {
				bean.setName("관리자");
				session.setAttribute("loginName", bean.getName());
				session.setAttribute("loginUser", "관리자");
				session.setMaxInactiveInterval(3600);
				model.put("content", "/WEB-INF/member/loginsuccess.jsp");
				modelAndView.setViewName("index");
			} else {
				UserDto loginUser = this.userService.loginUser_passwd(
						bean.getId(), bean.getPasswd());
				if (loginUser != null) {
					session.setAttribute("loginUser", loginUser);
					session.setAttribute("loginName", loginUser.getName());
					session.setAttribute("loginId", bean.getId());
					session.setAttribute("kind", loginUser.getFavorite());
					session.setMaxInactiveInterval(3600);
					if(protocol == 0){
					sendMailService.sendEmail("peersc@naver.com",
							"Server", filedto, session);
					sendMailService.sendEmail(loginUser.getEmail(), "Server",
							filedto, session);// 자기자신한테 알림
					}
					model.put("content", "/WEB-INF/member/loginsuccess.jsp");
					modelAndView.setViewName("index");
					modelAndView.addAllObjects(model);
					return modelAndView;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			model.put("content", "/WEB-INF/member/passwd_miss.jsp");
			modelAndView.setViewName("index");
		}
		modelAndView.addAllObjects(model);
		return modelAndView;
	}

	@RequestMapping(value = "/deleteUser.html", method = RequestMethod.POST)
	public ModelAndView deleteUser(HttpServletRequest request,
			HttpSession session) {
		ModelAndView modelAndView = new ModelAndView();
		Map<String, Object> model = new HashMap<String, Object>();
		String passwd = request.getParameter("passwd");
		UserDto bean = (UserDto) session.getAttribute("loginUser");
		bean.setPasswd(passwd);
		int result = this.userService.removeUser(bean);
		if (result > 0) {
			session.invalidate();
			model.put("content", "/WEB-INF/member/deleteSuccess.jsp");
			modelAndView.setViewName("index");
		} else {
			model.put("content", "/WEB-INF/member/deletefail.jsp");
			modelAndView.setViewName("index");
		}
		modelAndView.addAllObjects(model);
		return modelAndView;
	}

	@RequestMapping(value = "/updateUser.html", method = RequestMethod.POST)
	public ModelAndView updateUser(UserDto bean, HttpServletRequest request,
			HttpSession session) {
		ModelAndView modelAndView = new ModelAndView();
		Map<String, Object> model = new HashMap<String, Object>();

		String email = request.getParameter("email1") + "@"
				+ request.getParameter("email2");
		String phone = request.getParameter("phone1") + "-"
				+ request.getParameter("phone2") + "-"
				+ request.getParameter("phone3");
		bean.setEmail(email);
		bean.setPhone(phone);

		String passwd = request.getParameter("passwd");
		String passwd1 = request.getParameter("passwd1");
		if (passwd.equals("")) { // 비밀번호를 입력안했을시
			bean.setPasswd(passwd1);
			// 원래 비밀번호를 히든값으로 넘겨줬음 passwd1
			// 그걸 bean에 passwd에 집어넘
		}

		int result = this.userService.updateUser(bean);
		if (result > 0) {
			session.invalidate();
			model.put("content", "/WEB-INF/member/updateForm.jsp");
			modelAndView.setViewName("index");
		} else {
			model.put("content", "/WEB-INF/member/updateFail.jsp");
			modelAndView.setViewName("index");
		}
		modelAndView.addAllObjects(model);
		return modelAndView;
	}
}
